package moduleFrame
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import moduleFrame.interfaces.IListener;
	import moduleFrame.interfaces.ISender;

	/**
	 * 模块类，模块添加在这里管理
	 * @author Pelephone
	 */
	public class Context extends MapComposite implements ISender,IListener
	{
		protected var _contextView:DisplayObject;
		/**
		 * 单例映射<String,Object>
		 * 用绑定反射是为了方便回收管理，例如我dispose此模块，则对应的单例也会批量删除
		 */
		private var singleMap:Object;
		
		public function Context(aliasName:String=null,aContextView:DisplayObject=null)
		{
			_contextView = aContextView;
			singleMap = {};
			
			super(aliasName);
		}
		
		/**
		 * 监听全局消息
		 */
		public function addRootListener(type:String, listener:Function, useCapture:Boolean = false, 
										priority:int = 0, useWeakReference:Boolean = false):void
		{
			getMapRoot().addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		/**
		 * 移除全局消息的监听
		 * @param type
		 * @param listener
		 * @param useCapture
		 */
		public function removeRootListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			getMapRoot().removeEventListener(type,listener,useCapture);
		}
		
		/**
		 * 监听模块消息（只当前自己模块内消息有效）
		 */
		public function addModuleListener(type:String, listener:Function, useCapture:Boolean = false,
										  priority:int = 0, useWeakReference:Boolean = false):void
		{
			getMapParent().addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		/**
		 * 移除模块消息的监听
		 * @param type
		 * @param listener
		 * @param useCapture
		 */
		public function removeModuleListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			getMapParent().removeEventListener(type,listener,useCapture);
		}
		
		/**
		 * 托管全局事件
		 */
		public function dispatchRootEvent(event:Event):Boolean
		{
			return getMapParent().dispatchEvent(event);
		}
		
		/**
		 * 托管模块事件(只当前模块内消息有效)
		 */
		public function dispatchModuleEvent(event:Event):Boolean
		{
			return getMapParent().dispatchEvent(event);
		}
		
		/**
		 * 获取映射在框架上的单例
		 * @return 
		 
		protected function getMapObject(aliasName:String):IMaper
		{
			// 先从全局上查是否有映射存在
			var res:IMaper = getMapParent().getChildMaper(aliasName);
			if(res) return res;
			// 全局没有再从本模块中找
			return getMapParent().getChildMaper(aliasName);
		}*/
		
		/**
		 * 获取映射在框架上的单例
		 * @return 
		 
		protected function getMapObjectOf(refObj:Object):*
		{
			// 先从全局上查是否有映射存在
			var res:IMaper = getMapParent().getChildMaperOf(refObj);
			if(res) return res;
			// 全局没有再从本模块中找
			return getMapParent().getChildMaperOf(refObj);
		}*/
		
		/**
		 * 覆盖获取单例
		 * 通过类Class获取单例，此方法会自动生成对象，并绑定在跟节点;
		 * @param cls
		 * @return 
		 */
		override public function getMapSinglen(cls:Class):*
		{
			var aliasName:String = getQualifiedClassName(cls);
			var resObj:Object = singleMap[aliasName];
			if(resObj) return resObj;
			resObj = new cls();
			if(!resObj) return null;
			singleMap[aliasName] = resObj;
			return resObj;
		}
		
		override public function dispose():void
		{
			for (var sName:String in singleMap) 
			{
				var single:Object = singleMap[sName];
				var diFun:Function = single["dispose"];
				singleMap[sName] = null;
				delete singleMap[sName];
				if(diFun==null) continue;
				diFun.apply(null,[]);
			}
			singleMap = null;
			_contextView = null;
			
			super.dispose();
		}

		/**
		 * 关联的显示对象
		 */
		public function get contextView():DisplayObject
		{
			return _contextView;
		}

		public function set contextView(value:DisplayObject):void
		{
			_contextView = value;
		}
	}
}